package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpExprService exprService;

    @Override
    public PageResult queryPage(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());

        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper();
        wrapper.like(param.getName() != null && param.getName().isEmpty(),Emp::getName, param.getName())
                .eq(param.getGender() != null,Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd());

        IPage page1 = this.page(page, wrapper);

        List<Emp> emps = page1.getRecords();
        List<Integer> deptids = emps.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());
        List<Dept> depts = deptMapper.selectBatchIds(deptids);
        emps.forEach(emp ->{
            depts.forEach(dept ->{
                if (emp.getDeptId() != null && Objects.equals(emp.getDeptId(), dept.getId())){
                    emp.setDeptName(dept.getName());
                }
            });
        });
        return new PageResult(page1.getTotal(), emps);
    }

    @Override
    @Transactional
    public void saveAll(Emp emp) {
        empMapper.insert(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr ->{
            expr.setEmpId(emp.getId());
        });
        empExprMapper.insert(exprList);
    }

    @Override
    @Transactional
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByIds(ids);
    }

    @Override
    public Emp queryById(Integer id) {
        Emp emp = empMapper.selectById(id);
        List<EmpExpr> list = exprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id));
        emp.setExprList(list);
        return emp;
    }

    @Override
    @Transactional
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

        exprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        List<EmpExpr> exprList = emp.getExprList();

        if (exprList != null && exprList.size() > 0){
            exprList.forEach(expr ->{
                expr.setEmpId(emp.getId());
            });
            exprService.saveBatch(exprList);
        }
    }
}
